#include "repairhistorydialog.h"
#include "ui_repairhistorydialog.h"
#include<QSqlQueryModel>
#include"mes.h"
#include<QDebug>
#include<QMessageBox>
#include<QFileDialog>
#include<QFile>

RepairHistoryDialog::RepairHistoryDialog(QWidget *parent,QString NO,QString workNumber) :
    QDialog(parent),
    ui(new Ui::RepairHistoryDialog)
{
    ui->setupUi(this);
    this->NO=NO;
    this->workNumber=workNumber;
    setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
    initTableView();
}

RepairHistoryDialog::~RepairHistoryDialog()
{
    delete ui;
}

void RepairHistoryDialog::initTableView()
{
    QSqlQueryModel *model=new QSqlQueryModel(this);
    QString sql=QString("SELECT TOP 200 a.[NO],b.norms \
                        ,a.[workNumber] \
                        ,a.[textReason] \
                        ,a.[date]   \
                        ,a.[status] \
                        ,a.[userNumber] \
                    FROM [dm-data].[dbo].[repairForm] as a inner join importForm as b \
                    on a.NO=b.NO where a.NO='%1' and a.userNumber='%2'").arg(NO).arg(workNumber);
    qDebug()<<sql;
    model->setQuery(sql);
    if(model->rowCount()==0){
        ui->msg->setText("该设备无维修历史");
        ui->pushButton_2->setEnabled(false);
        return;
    }

    model->setHeaderData(0,Qt::Horizontal,QString("设备编号"));
    model->setHeaderData(1,Qt::Horizontal,QString("设备名称"));
    model->setHeaderData(2,Qt::Horizontal,QString("报修人工号"));
    model->setHeaderData(3,Qt::Horizontal,QString("报修原因"));
    model->setHeaderData(4,Qt::Horizontal,QString("报修日期"));
    model->setHeaderData(5,Qt::Horizontal,QString("历史状态"));
    model->setHeaderData(6,Qt::Horizontal,QString("保管人工号"));
    ui->tableView->setModel(model);

    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

void RepairHistoryDialog::on_pushButton_clicked()
{
    reject();
}

void RepairHistoryDialog::on_pushButton_2_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,tr("Save File"),QString("%1(%2)").arg(NO).arg(workNumber),tr("file (*.csv)"));
    if (!fileName.isEmpty())
    {
        QString textData;
        int rows = ui->tableView->model()->rowCount();
        int columns = ui->tableView->model()->columnCount();
        for(int j=0;j<columns;j++){
            textData+=ui->tableView->model()->headerData(j,Qt::Horizontal,Qt::DisplayRole).toString();
            textData += "," ;
        }
        textData += "\n";
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < columns; j++){
                textData += ui->tableView->model()->data(ui->tableView->model()->index(i,j)).toString().replace(",",";");
                textData += "," ;     // for .csv file format
            }
            textData += "\n";             // (optional: for new line segmentation)
        }
        // [Save to file] (header file <QFile> needed)
        // .csv
        QFile csvFile(fileName);
        if(csvFile.open(QIODevice::WriteOnly | QIODevice::Truncate)){
            QTextStream out(&csvFile);
            out << textData;
            csvFile.close();
        }
    }
}
